
// 模拟红灯亮
function red() {
    console.log('red')
}

//模拟绿灯亮
function green() {
    console.log('green')
}

//模拟黄灯亮
function yellow() {
    console.log('yellow')
}

const timer = (light,time) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            if (light === 'red') {
                red()
            }
            else if (light === 'yellow') {
                yellow()
            }
            else {
                green()
            }
            resolve()
        },time)
    })
}
//? promise
const task = () => {
    timer('red',3000).then(() => timer('yellow',1000)).then(() => timer('green',2000)).then(() => timer('yellow',1000)).then(task)
}
// task()

//? async await
async function light() {
    await timer('red',3000)
    await timer('yellow',1000)
    await timer('green',2000)
    await timer('yellow',1000)
    await light()
}

light()


